﻿using System;
using System.Windows.Forms;
using EvoGame.Core.Files;

namespace EvoGame.Core.Components
{
	public partial class DebugConsole : UserControl
	{
		public string Output
		{
			get { return debugConsole.Text; }
			set
			{
				this.debugConsole.AppendText ("\t------------" + DateTime.UtcNow.ToLongTimeString () + "-------------" + Environment.NewLine);
				this.debugConsole.AppendText (value + Environment.NewLine);
				this.logFile.Write ("\t------------" + DateTime.UtcNow.ToLongTimeString () + "-------------" + Environment.NewLine);
				this.logFile.Write (value + Environment.NewLine);
			}
		}

		private static bool Active = false;
		private FileHandler.Log logFile;

		public DebugConsole()
		{
			this.InitializeComponent ();
			Active = true;
			EvoApp.Debug = this;
			this.Dock = DockStyle.Fill;
			this.logFile = new FileHandler.Log (String.Format ("{0}{2}debug_{1}.log", EvoApp.Path, DateTime.UtcNow.ToString ("yyMMddHHmmss"), System.IO.Path.DirectorySeparatorChar));
			this.Output = "Debug system engaged." + Environment.NewLine + "Architecture found: " +
						  Environment.GetEnvironmentVariable ("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine);
		}

		public void SaveLog()
		{
			/*FileStream log = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read);
			TextWriter logWrite = new StreamWriter(log);
			logWrite.Write(Output);
			logWrite.Close();*/
			this.logFile.Save ();
			this.logFile.Close ();
		}

		public static implicit operator bool(DebugConsole dc)
		{
			return Active;
		}
	}
}
